﻿using System;
using System.Globalization;
using System.Runtime.Serialization;

namespace Bookvar.ObjectModel.Actions.Collaboration
{
    [Serializable]
    [DataContract]
    public class RemoveTopicAction : ActionBase
    {
        private Guid removeTopicId;

        [DataMember]
        public Guid RemoveTopicId
        {
            get { return removeTopicId; }
            set { removeTopicId = value; }
        }

        public RemoveTopicAction( Guid removeTopicId )
        {
            this.removeTopicId = removeTopicId;
        }

        public override void Execute( IExecutionContext context )
        {
            if ( context == null )
            {
                throw new ArgumentNullException( "context" );
            }
            if ( RemoveTopicId == Guid.Empty )
            {
                throw new InvalidOperationException( "RemoveTopicId is not properly set" );
            }

            try
            {
                context.ExecutionMode = ExecutionMode.Execution;
                Topic toRemove = context.MainTopic.FindTopic( RemoveTopicId );
                toRemove.Parent.SubTopics.Remove( toRemove );
            }
            finally
            {
                context.ExecutionMode = ExecutionMode.None;
            }
        }

        public override string ToString()
        {
            return string.Format(CultureInfo.CurrentCulture, "RemoveTopicId: {0}", RemoveTopicId );
        }
    }
}